<?php

class Member_extendController extends AdminController {

    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function actionUnpaid_list() {
        $model = new sys_member_activation_request('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['sys_member_activation_request']))
            $model->attributes = $_GET['sys_member_activation_request'];

        $this->render('unpaid_list', array(
            'model' => $model,
        ));
    }

    public function actionUpdateUnpaid($id) {
        if (!empty($id)) {
            $update = "UPDATE sys_member_activation_request SET member_activation_request_status='paid' where member_activation_request_id = '" . $id . "'";
            //die($update);
            $command = Yii::app()->db->createCommand($update);
            //$command->execute();
            if ($command->execute()) {
                $this->redirect(array('Unpaid_list'));
            } else {
                echo "gagal" . mysql_error();
            }
        }

//        if (isset($_POST['sys_member_activation_request'])) {
//            if ($model->save())
//                $this->redirect(array('view', 'id' => $model->no_op));
//        }
    }

    public function actionCancel($id) {
        if (!empty($id)) {
            $update = "UPDATE sys_member_activation_request SET member_activation_request_status='unpaid' where member_activation_request_id = '" . $id . "'";
            //die($update);
            $command = Yii::app()->db->createCommand($update);
            //$command->execute();
            if ($command->execute()) {
                $this->redirect(array('paid_list'));
            } else {
                echo "gagal" . mysql_error();
            }
        }

//        if (isset($_POST['sys_member_activation_request'])) {
//            if ($model->save())
//                $this->redirect(array('view', 'id' => $model->no_op));
//        }
    }

    public function actionTriggerPerpanjangan($id) {
        date_default_timezone_set("Asia/Jakarta");
        if (!empty($id)) {

            $user=Yii::app()->user->administrator_id;
            //die($user);
            
            $idReq = sys_member_activation_request::model()->findByPk($id);
            $network_id=$idReq->member_activation_request_network_id;
            $quantity=$idReq->member_activation_request_quantity;
            $date_request=$idReq->member_activation_request_datetime;
            $date_request_id=date_format(date_create($date_request), 'd M Y');
            $datetime = date("Y-m-d H:i:s");

            $start_date = Yii::app()->db->createCommand("SELECT max(member_activation_end_date) FROM sys_member_activation WHERE member_activation_network_id = '".$network_id."'")->queryScalar();

            $insert="INSERT INTO sys_member_activation SET 
            member_activation_network_id = '" . $network_id . "', 
            member_activation_start_date = '" . $start_date . "', 
            member_activation_end_date = DATE_ADD('" . $start_date . "', INTERVAL '" . $quantity . "' YEAR), 
            member_activation_note = 'Perpanjangan Masa Aktif dari Request tanggal " .$date_request_id. "',
            member_activation_input_datetime = '" . $datetime . "',
            member_activation_input_administrator_id = '" . $user . "'";

            //die($insert);
            $command = Yii::app()->db->createCommand($insert);
            //$command->execute();
            if ($command->execute()) {
                
            $update = "UPDATE sys_member_activation_request SET member_activation_request_status='approved' where member_activation_request_id = '" . $id . "'";
            $approve = Yii::app()->db->createCommand($update);
            $approve->execute();
                $this->redirect(array('paid_list'));
            } else {
                echo "gagal" . mysql_error();
            }
        }

//        if (isset($_POST['sys_member_activation_request'])) {
//            if ($model->save())
//                $this->redirect(array('view', 'id' => $model->no_op));
//        }
    }

    public function actionPaid_list() {
        $model = new sys_member_activation_request('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['sys_member_activation_request']))
            $model->attributes = $_GET['sys_member_activation_request'];

        $this->render('paid_list', array(
            'model' => $model,
        ));
    }

    public function actionReportActivation() {
        $model = new sys_member_activation_request('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_GET['sys_member_activation_request']))
            $model->attributes = $_GET['sys_member_activation_request'];

        $this->render('report_activation', array(
            'model' => $model,
        ));
    }

    public function loadModel($id) {
        $model = sys_member_activation_request::model()->findByPk($id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    public function actionDelete($id) {
        $this->loadModel($id)->delete();

        // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
        if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('unpaid_list'));
    }

    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'sys-member-activation-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
?>

